<template>
  <div class="setting-takegoods-container">
    <el-form :model="takeGoodsData" label-width="100px" size="small" style="margin: 20px 100px">
      <el-form-item label="退款电话">
        <el-input v-model="takeGoodsData.after_phone" placeholder="请输入" style="width: 40%"/>
      </el-form-item>
      <el-form-item label="自提点地区">
        <el-cascader :options="options" v-model="selectedOptions" @change="handleChange" style="width:250px" placeholder="请选择省市区"></el-cascader>
      </el-form-item>
      <el-form-item label="退货地址">
        <el-input v-model="takeGoodsData.address" type="textarea" placeholder="请输入" style="width: 40%"/>
      </el-form-item>
      <el-form-item label="店铺地址">
        <el-input v-model="takeGoodsData.store_address" type="textarea" placeholder="请输入" style="width: 40%"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="takeGoodsData.status" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center;margin-top:15px">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { getTakeGoods, postTakeGoods, putTakeGoods } from '@/api/setting'
import { regionData, CodeToText } from 'element-china-area-data'

export default {
  name: 'setting-takegoods',
  data() {
    return {
      takeGoodsData: {
        after_phone: '',
        after_addr: '',
        status: '1',
        key: this.$store.state.app.activeApp.saa_key
      },
      isAdd: true,
      options: regionData,
      tuanData: {},
      selectedOptions: [],
    }
  },
  watch: {
      tuanData(newData) {
          this.selectedOptions = [newData.province_code,newData.city_code,newData.area_code];
      }
  },
  mounted() {
    this.getTakeGoods();
  },
  methods: {
    /**
     * 获取收货信息
     */
    getTakeGoods() {
      getTakeGoods({key: this.$store.state.app.activeApp.saa_key}).then(res => {
        if(res.status === 200) {
          this.takeGoodsData = res.data[0];
          this.selectedOptions = [res.data[0].province_code,res.data[0].city_code,res.data[0].area_code];
          this.isAdd = false;
        }else if(res.status === 204) {
          this.takeGoodsData = {
            after_phone: '',
            after_addr: '',
            status: '1',
            key: this.$store.state.app.activeApp.saa_key
          };
          this.isAdd = true;
        }
        else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 省市联动
     */
    handleChange (areaCodeList) {
        this.takeGoodsData.province_code = areaCodeList[0];
        this.takeGoodsData.province = CodeToText[areaCodeList[0]];

        this.takeGoodsData.city_code = areaCodeList[1];
        this.takeGoodsData.city = CodeToText[areaCodeList[1]];

        this.takeGoodsData.area_code = areaCodeList[2];
        this.takeGoodsData.area = CodeToText[areaCodeList[2]];
    },
    submit() {
      delete this.takeGoodsData.delete_time;
      if(this.isAdd) {
        postTakeGoods(this.takeGoodsData).then(res => {
          if(res.status === 200) {
            this.$message.success('添加成功！');
            // this.getTakeGoods();
          }else{
            this.$message.error(res.message);
          }
        })
      }else{
        putTakeGoods(this.takeGoodsData).then(res => {
          if(res.status === 200) {
            this.$message.success('修改成功！');
            // this.getTakeGoods();
          }else{
            this.$message.error(res.message);
          }
        })
      }
    }
  },
}
</script>

<style lang="scss" scoped>
.setting-takegoods-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
</style>
